package com.zhipu.resetbalance.ui.home.calibrate;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.zhipu.resetbalance.common.Constants;
import com.zhipu.resetbalance.listener.SerialPortListener;
import com.zhipu.resetbalance.utils.InitializeUtils;
import com.zhipu.resetbalance.utils.popup.PopupUtils;
import com.zhipu.resetbalance.utils.serialPort.WeightSerialPortUtils;
import com.zhipu.resetbalance.utils.weight.WeightStringUtils;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class CalibrateViewModel extends ViewModel implements SerialPortListener {

    /**
     * 称重重量
     */
    public ObservableField<String> WeighingWeight = new ObservableField<>("0");

    /**
     * 文字提示
     */
    public ObservableField<String> TextPrompt = new ObservableField<>("");



    /**
     * 取消校准
     */
    public ObservableField<Boolean> uncalibrate = new ObservableField<>(false);


    /**
     * 定时器
     */
    private Disposable mDisposable;

    /**
     * 操作中
     */
    private boolean InOperation = false;

    /**
     *
     */
    public void milliseconds(){
        /**
         * 设置回调
         */
        WeightSerialPortUtils.setSerialPortListener(this);
        /**
         *
         */
        mDisposable = Observable.interval(1, TimeUnit.MILLISECONDS).subscribe(number ->{
            if(!InOperation){
                /**
                 * 重量輸出命令
                 */
                WeightSerialPortUtils.WeightOutputCommand();
            }
        });
    }

    /**
     * 释放定时器
     */
    public void ReleaseTimer(){
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }

    /**
     * 去皮
     */
    public void peel(){
        //操作中
        InOperation = true;
        /**
         * 显示弹框
         */
        PopupUtils.showBeCommonPopup(ActivityUtils.getTopActivity());
        PopupUtils.SettingBeCommonPopup("正在去皮请稍后...");
        /**
         * 置零命令
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                WeightSerialPortUtils.ZeroOrder();
            }
        }).start();

    }


    /**
     * 取消校准
     */
    public void uncalibrate(){
        //操作中
        InOperation = true;
        /**
         * 取消校准
         */
        uncalibrate.set(true);
        /**
         * 显示弹框
         */
        PopupUtils.showBeCommonPopup(ActivityUtils.getTopActivity());
        PopupUtils.SettingBeCommonPopup("正在取消定标请稍后...");
        /**
         * 取消定标
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                WeightSerialPortUtils.cancelCalibration();
            }
        }).start();


    }

    /**
     * 校准
     */
    public void clickCalibration(){
        //操作中
        InOperation = true;
        /**
         * 显示弹框
         */
        PopupUtils.showBeCommonPopup(ActivityUtils.getTopActivity());
        PopupUtils.SettingBeCommonPopup("校准中请稍等...");
        //綫性標定命令
        //WeightSerialPortUtils.LinearCalibrationCommand();
        new Thread(new Runnable() {
            @Override
            public void run() {

                WeightSerialPortUtils.Calibration500();

            }
        }).start();

    }


    public void calibration(){


    }

    /**
     * 主菜单
     */
    public void mainMenu(){
        /**
         * 释放
         */
        ReleaseTimer();
        /**
         *
         */
        ActivityUtils.finishActivity(CalibrateActivity.class);
    }

    @Override
    public void itemClicked(byte[] event) {
        //byte[]转字符串
        String numericalValue = ConvertUtils.bytes2HexString(event);
//        System.out.println(numericalValue);
        /**
         *
         */
        if(numericalValue.equals(Constants.Calibration100)){//標定100
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码100");
            TextPrompt.set("请放砝码100g");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.Calibration200)){//標定200
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码200");
            TextPrompt.set("请放砝码200g");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.Calibration500)){//標定500
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码500");
            TextPrompt.set("请放砝码500g");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.Calibration1000)){//標定1000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码1000");
            TextPrompt.set("请放砝码1000g");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.Calibration2000)){//標定2000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码2000");
            TextPrompt.set("请放砝码2000g");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.Calibration3000)){//標定3000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码3000");
            TextPrompt.set("请放砝码3000g");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.SEND_GO_START)){//去皮完成
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("去皮成功");
            TextPrompt.set("去皮成功");
            //操作中
            InOperation = false;
        }else if(numericalValue.equals(Constants.overweight)){//超重
            WeighingWeight.set("超重");
            TextPrompt.set("超重");
        }else if(numericalValue.equals(Constants.SHIELD)){//校准中
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("校准中");
            TextPrompt.set("校准中请稍等...");
            //取消校准
            uncalibrate.set(true);
        }else if(numericalValue.equals(Constants.CalibrationComplete)){//校准完成
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("校准完成");
            TextPrompt.set("校准完成");
            //操作中
            InOperation = false;
            //取消校准
            uncalibrate.set(false);
        }else {
            String weightHandle = WeightStringUtils.weightHandle(event);
            /**
             * 打印重量
             */
            WeighingWeight.set(weightHandle);
            TextPrompt.set("");
        }
    }
}
